<template>
  <el-container>
    <el-main style="margin-top: -20px">
      <!-- 操作栏 -->
      <div style="background: #d6ebf1; height: 40px">
        <span>查询记录</span>
        <el-date-picker
          v-model="value1"
          size="mini"
          align="center"
          type="daterange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        >
        </el-date-picker>

        <el-button size="mini">本班次</el-button>
        <el-button size="mini">今天</el-button>
        <el-button size="mini">全部</el-button>

        <span>操作时间</span>
        <el-date-picker
          v-model="operationTime"
          type="datetime"
          size="mini"
          placeholder="选择时间"
        >
        </el-date-picker>

        <el-button size="mini">新建</el-button>
        <el-button size="mini" @click="submit()">提交</el-button>
        <el-button size="mini" @click="temporarySave()">暂存</el-button>
        <el-button size="mini">删除</el-button>
        <el-button size="mini">刷新</el-button>
        <el-button size="mini">检查检验</el-button>
        <el-button size="mini" disabled>打印</el-button>
      </div>

      <!-- 各种病例 -->
      <el-tabs v-model="activeName" @tab-click="handleClick" type="border-card">
        <el-tab-pane
          v-for="(module, index) in eva_overall"
          :key="index"
          :label="module[0]"
          :name="index.toString()">
          <el-form label-position="right" ref="form" :model="form" label-width="100px">
            <el-row v-for="(subModule, i) in module" :key="i">
              <!-- submodule -->
              <div class="submoduleName" v-if="i != 0 && subModule.length != 1" style="background: #d6ebf1; height: 30px">
                <p style="font-weight: bold">{{ subModule[0] }}:</p>
              </div>
              <!-- submodule的item -->
              <el-checkbox-group v-if="i != 0" v-model="form.data[index]">
                <el-col :span="4" v-for="(item, j) in subModule" :key="j">
                  <el-checkbox v-if="j != 0" :label="index + ',' + i + ',' + j">{{ item[0] }}</el-checkbox>
                </el-col>
              </el-checkbox-group>
            </el-row>
          </el-form>
        </el-tab-pane>
      </el-tabs>

    </el-main>
  </el-container>
</template>

<script>


var BaaS = require("minapp-sdk");
let clientID = "9ed0a3da89683e42d793";
BaaS.init(clientID);

export default {
  name: "overallEvaluation",
  data() {
    return {
      id: "62e5e3f691261574891d176f",
      value1: "",
      operationTime: "",
      activeName: "0",
      // activeIndex: 1,
      moduleListSet: new Set(),
      moduleList: [],
      submoduleListSet: new Set(),
      submoduleList: [],
      // 存放整体评估所有项的三维数组[[module,code],[submodule,code],[item,value]]
      eva_overall: [],
      allData: [],
      form: {
        data: [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],],
      },
    };
  },
  methods: {
    handleClick(tab, event) {
      // console.log(tab);
      // console.log(event);
      //   this.temporarySave();
      console.log("处理暂存");
    },
    // 获取数据库的整体评估指标
    getEva_overall(index) {
      let that = this;
      let query = new BaaS.Query();
      let overall_eval = new BaaS.TableObject("overall_eval_item");
      overall_eval
        .setQuery(query)
        .select([
          "module_num",
          "module",
          "submodule_num",
          "submodule",
          "item",
          "item_code",
        ])
        .limit(1000)
        .offset(index)
        .find()
        .then((res) => {
          that.allData.push(...res.data.objects);
          if (res.data.meta.next !== null) {
            that.getEva_overall(index + 1000);
          } else {
            that.formatDate();
            // console.log(that.allData);
          }
        });
    },
    // 将数据库获取的整体评估指标转为数组存储
    formatDate() {
      this.eva_overall = [];
      this.allData.forEach((data) => {
        this.moduleListSet.add(data.module);
        this.submoduleListSet.add(data.submodule);
      });

      this.submoduleList = Array.from(this.submoduleListSet);
      this.moduleList = Array.from(this.moduleListSet);
      for (let i = 0; i < this.moduleList.length; i++) {
        //set类型数据无法直接获取长度 需要转成数组
        let tempsubmoduleList = [];
        //初始化submoduleList['module',[submodule,..],[submodule,..],[submodule,..],[submodule,..],[submodule,..]]
        for (let j = 0; j < this.submoduleList.length; j++) {
          if (j == 0) {
            tempsubmoduleList.push(this.moduleList[i]); //'活动'
            let temparr = [this.submoduleList[j]]; // 定义性特征
            tempsubmoduleList.push(temparr);
          } else {
            let temparr = [this.submoduleList[j]]; // submodule
            tempsubmoduleList.push(temparr);
          }
        }
        //填充submoduleList['A',[
        //         1,
        //         ['步态改变','e.A.1.1'],
        //         ['步态改变','e.A.1.1'],
        //         ...
        //     ],[
        //         2,
        //        ['步态改变','e.A.1.1'],
        //        ['步态改变','e.A.1.1'],
        //        ..
        //     ],
        //     ...
        // ]
        this.allData.forEach((data) => {
          if (data.module_num == String.fromCharCode(65 + i)) {
            let itemList = [];
            itemList.push(data.item, data.item_code, 0); //默认都是未选中
            tempsubmoduleList[data.submodule_num].push(itemList);
          }
        });
        this.eva_overall.push(tempsubmoduleList);
      }
      console.log(this.eva_overall);
    },
    // 将指标转为目标json字符串
    getEvaOverallJson() {
      let submitDate = {};
      for (let i = 0; i < this.eva_overall.length; i++) {
        //module 数量
        for (let j = 1; j < this.eva_overall[i].length; j++) {
          // 每个module 有五个submodule
          for (let k = 1; k < this.eva_overall[i][j].length; k++) {
            //每个submodule的item数目
            // console.log(this.eva_overall[i][j][k]);
            submitDate[this.eva_overall[i][j][k][1]] =
              this.eva_overall[i][j][k][2];
          }
        }
      }
      let submitLast = JSON.stringify(submitDate).replaceAll('"', '\\"');
      // console.log(submitLast);
      return submitLast;
    },
    // 更新整体评估指标
    updateEvaOverall(data) {
      for (let i = 0; i < data.length; i++) {
        let arr = data[i].split(",");
        this.eva_overall[arr[0]][arr[1]][arr[2]][2] = 1;
        console.log(this.eva_overall[arr[0]][arr[1]][arr[2]]);
      }
    },
    // 暂存
    temporarySave() {
      that.$refs.form[that.activeName].validate((valid) => {
        //每个tab页面都是一个form
        if (valid) {
          this.$confirm("确认暂存吗？", "提示", {})
            .then(() => {
              console.log(that.form.data[this.activeName]);
              //更新当前模块下的数据：传入勾选项的'moduel_index,submodel_index,item_index'
              this.updateEvaOverall(that.form.data[this.activeName]);
              this.$message({
                message: "暂存成功",
                type: "success",
              });
            })
            .catch((e) => {
              console.log(e);
            });
        }
      });
    },
    // 提交
    submit() {
      let that = this;
      this.$confirm("确定已完成所有指标评估，要提交给AI吗？否则先点击暂存", "提示",{})
        .then(() => {
          //  获取json字符串
          let eva_overall = that.getEvaOverallJson();
        //   let eva_overall = '{\"e.A.1.1\":0}';
        //   提交到数据库
          let patientNursing = new BaaS.TableObject("patient_nursing");
          let record = patientNursing.getWithoutData(that.id);
          record.set({'eva_overall':eva_overall})
          record.update().then(res => {
            console.log(res);
            this.$message({
            message: "提交成功",
            type: "success"
            });
          }, err => {
              console.log(err);
          })
        })
        .catch((e) => {
          console.log(e);
        });
    },
    init() {
      this.getEva_overall(0);
    },
  },
  created() {
    this.init();
  },
};
</script>
<style lang="less" scoped>
:deep(.el-tabs__nav) {
  display: flex !important;
  flex-wrap: wrap !important;
}
:deep(.el-tabs__content) {
  padding-top: 0px;
}
:deep(.el-main) {
  padding: 20px 2px 10px 2px;
}
</style>



